<?php
namespace app\trade\controller;

use app\model\bank\BankModel;
use app\model\book\BookModel;
use app\model\book\OrderModel;
use app\model\user\UserModel;
use think\facade\Db;
use think\facade\Request;
class Index
{

    public function index()
    {
        pp('Hello!');
    }

    // 规上服务业营业收入统计
    public function incomeStatistics() {
        $post['year'] = input('post.year','2022');
        $post['month'] = input('post.month','1');
        try {
            $month = intval($post['month']);
            checkPost($post);
            if(($month < 1 || $month > 12) && $month!==0) {
                return ajax('',-1,'无效的参数');
            }
            $where = [
                ['year','=',$post['year']]
            ];
            $list = Db::table('area_data')->where($where)->select()->toArray();
            $res = [
                [
                    'trade_id' => 1,
                    'trade_name' => '运输业',
                    'income' => 0,
                    'income_last_year' => 0,
                    'rate' => 0,
                ],
                [
                    'trade_id' => 2,
                    'trade_name' => '多式联运和运输代理业',
                    'income' => 0,
                    'income_last_year' => 0,
                    'rate' => 0,
                ],
                [
                    'trade_id' => 3,
                    'trade_name' => '装卸搬运和仓储业',
                    'income' => 0,
                    'income_last_year' => 0,
                    'rate' => 0,
                ],
            ];
            foreach ($res as &$trade) {
                foreach ($list as $row) {
                    if($row['trade'] === $trade['trade_id']) {
                        if($month > 0) {
                            $trade['income'] += $row['month'.$month];
                            $trade['income_last_year'] += $row['month'.$month.'_last_year'];
                        }else {
                            for($i=2;$i<=12;$i++) {
                                $trade['income'] += $row['month'.$i];
                                $trade['income_last_year'] += $row['month'.$i.'_last_year'];
                            }
                        }
                    }
                }
                $trade['rate'] = $trade['income_last_year'] > 0 ? round(($trade['income']-$trade['income_last_year'])/$trade['income_last_year'],4) : 0;
                $trade['rate'] = round($trade['rate'] * 100,2);
            }

        } catch (\Exception $e) {
            return ajax($post,-1,$e->getMessage());
        }
        return ajax($res);

    }

    // 物流项目建设情况统计
    public function seasonData() {
        $post['year'] = input('post.year','2023');
        $post['season'] = input('post.season','1');
        try {
            $season = intval($post['season']);
            checkPost($post);
            if(($season < 1 || $season > 4)) {
                return ajax('',-1,'无效的参数');
            }
            $where = [
                ['del','=',0],
                ['year','=',$post['year']],
                ['season','=',$season],
            ];
            $total_num = Db::table('project')->where($where)->count();
            $start_num = Db::table('project')->where($where)->where('status','=','是')->count();
            $important_num = Db::table('project')->where($where)->where('desc','<>','')->count();// 备注不为空就是重点项目
            $res = [
                'total_num' => $total_num,
                'start_num' => $start_num,
                'important_num' => $important_num,
            ];
        } catch (\Exception $e) {
            return ajax($post,-1,$e->getMessage());
        }
        return ajax($res);
    }

    // 各区县物流项目统计
    public function areaData() {
        $post['year'] = input('post.year','2023');
        $post['season'] = input('post.season','1');
        try {
            checkPost($post);
            $season = intval($post['season']);
            if(($season < 1 || $season > 4)) {
                return ajax('',-1,'无效的参数');
            }
            // 获取全部区县
            $area_list = Db::table('area_data')->field('area')->group('area')->select()->toArray();
            $where = [
                ['del','=',0],
                ['year','=',$post['year']],
                ['season','=',$season],
            ];
            // 获取年份季度的项目
            $project_list = Db::table('project')->where($where)->select()->toArray();
            foreach ($area_list as &$area) {
                $area['project_num'] = 0;
                $area['start_num'] = 0;
                $area['important_num'] = 0;
                foreach ($project_list as $project) {
                    if($project['area'] === $area['area']) {
                        $area['project_num'] += 1;
                        if($project['status'] === '是') {
                            $area['start_num'] += 1;
                        }
                        if($project['desc'] !== '') {
                            $area['important_num'] += 1;
                        }
                    }
                }
            }
        } catch (\Exception $e) {
            return ajax($post,-1,$e->getMessage());
        }

        return ajax($area_list);
    }

    // 物流项目类型统计
    public function typeData() {
        $post['year'] = input('post.year','2023');
        $post['season'] = input('post.season','1');
        $where = [
            ['del','=',0],
            ['year','=',$post['year']],
            ['season','=',$post['season']],
        ];
        try {
            // 获取全部区县
            $type_list = Db::table('project')->where($where)->field('project_type,count(project_type) AS num')->group('project_type')->select()->toArray();
        } catch (\Exception $e) {
            return ajax('',-1,$e->getMessage());
        }
        return ajax($type_list);
    }

    // 各区各类型企业数量统计
    public function companyNumData() {
        $year = input('param.year',date('Y'));
        try {
            // 获取全部区县
            $res = [
                [
                    'trade_id' => 1,
                    'trade_name' => '运输业',
                    'list' => [],
                ],
                [
                    'trade_id' => 2,
                    'trade_name' => '多式联运和运输代理业',
                    'list' => [],
                ],
                [
                    'trade_id' => 3,
                    'trade_name' => '装卸搬运和仓储业',
                    'list' => [],
                ],
            ];
            foreach ($res as &$trade) {
//                $trade['list'] = Db::table('area_data')->group('area,trade')->where('trade','=',$trade['trade_id'])->field('area,company_num')->select()->toArray();
                $trade['list'] = Db::table('area_data')->group('area,trade,company_num')
                    ->where('trade','=',$trade['trade_id'])
                    ->where('year','=',$year)
                    ->field('area,trade,company_num')->select()->toArray();
            }
        } catch (\Exception $e) {
            return ajax('',-1,$e->getMessage());
        }
        return ajax($res);
    }

    // 规上服务业营业收入统计按行业
    public function incomeStatisticsByArea() {
        $post['year'] = input('post.year','2022');
        $post['month'] = input('post.month','2');
        $post['trade_id'] = input('post.trade_id','1');
        try {
            checkPost($post);
            $month = intval($post['month']);
            $trade_id = intval($post['trade_id']);
            if(($month < 1 || $month > 12) && $month!==0) {
                return ajax('',-1,'无效的参数 month');
            }
            if(!in_array($trade_id,[1,2,3])) {
                return ajax('',-1,'无效的参数 trade');
            }
            // 获取全部区县
            $area_list = Db::table('area_data')->field('area')->group('area')->select()->toArray();
            $where = [
                ['year','=',$post['year']]
            ];
            $list = Db::table('area_data')->where($where)->select()->toArray();
            foreach ($area_list as &$area) {
                $area['income'] = 0;
                $area['income_last_year'] = 0;
                $area['rate'] = 0;
                foreach ($list as $row) {
                    if($row['trade'] === $trade_id && $row['area'] === $area['area']) {
                        if($month > 0) {
                            $area['income'] += $row['month'.$month];
                            $area['income_last_year'] += $row['month'.$month.'_last_year'];
                        }else {
                            for($i=2;$i<=12;$i++) {
                                $area['income'] += $row['month'.$i];
                                $area['income_last_year'] += $row['month'.$i.'_last_year'];
                            }
                        }
                    }
                }
                $area['rate'] = $area['income_last_year'] > 0 ? round(($area['income']-$area['income_last_year'])/$area['income_last_year'],4) : 0;
                $area['rate'] = round($area['rate'] * 100,2);
            }
        } catch (\Exception $e) {
            return ajax($post,-1,$e->getMessage());
        }
        return ajax($area_list);
    }


}
